package com.dhcc.cdc.task;

import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.util.KettleUtil;
import com.dhcc.core.framework.util.DateUtil;
import com.dhcc.core.framework.util.codec.AesUtil;
import org.pentaho.di.core.exception.KettleException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @ClassName:  DrawDataTask   
 * @Description:TODO(抽取数据的定时任务)   
 * @author: mazhaoyan 
 * @date:   2021年9月30日 上午10:04:45   
 *     
 * @Copyright: 2021 www.mediway.com.cn Inc. All rights reserved. 
 *
 */
@Service("drawDataTask")
public class DrawDataTask {
	@Value("${sys.sysHome}")
	private String sysHome;
	@Value("${sys.kettlePath}")
	private String kettlePath;
	@Value("${sys.jobFileName}")
	private String jobFileName;
	public void drawData(){
		//从各个信息系统同步数据
		try {
			// 获取昨天日期
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -1);
			Date yesterdayDate = calendar.getTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String startDate = formatter.format(yesterdayDate) + " 00:00:00";
			// 获取参数中的结束日期
			String endDate = formatter.format(yesterdayDate) + " 23:59:59";

			Map variables = new HashMap();
			//判断是cache 还是IRIR his数据库
			if (CdcConfigCache.me().getValueByKey("SDS", "his_dbtype", "").equals("IRIS")) {
				variables.put("his_driver", CdcConfigCache.me().getValueByKey("SDS","his_driver"));
				variables.put("his_url", CdcConfigCache.me().getValueByKey("SDS","his_url"));
			}
			// his服务器IP地址
			variables.put("his_ip", CdcConfigCache.me().getValueByKey("SDS", "his_ip"));
			// his数据库名称
			variables.put("his_ns", CdcConfigCache.me().getValueByKey("SDS", "his_ns"));
			// his数据库端口号
			variables.put("his_p", CdcConfigCache.me().getValueByKey("SDS", "his_p"));
			// his数据库用户名
			variables.put("his_u", CdcConfigCache.me().getValueByKey("SDS", "his_u"));
			// his数据库密码
			variables.put("his_pw", AesUtil.decode(CdcConfigCache.me().getValueByKey("SDS", "his_pw")));
			// sds服务器IP地址
			variables.put("sds_ip", CdcConfigCache.me().getValueByKey("SDS", "sds_ip"));
			// sds数据库端口号
			variables.put("sds_p", CdcConfigCache.me().getValueByKey("SDS", "sds_p"));
			// sds数据库名称
			variables.put("sds_s", CdcConfigCache.me().getValueByKey("SDS", "sds_s"));
			// sds数据库用户名
			variables.put("sds_u", CdcConfigCache.me().getValueByKey("SDS", "sds_u"));
			// sds数据库密码
			variables.put("sds_pw", AesUtil.decode(CdcConfigCache.me().getValueByKey("SDS", "sds_pw")));

			KettleUtil.runSDSJob("sds.kjb", variables, startDate,endDate, null, "1", false);
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}
	
	public void drawExpense(){
		//从各个信息系统同步数据
		try {
			// 获取昨天日期
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -1);
			Date yesterdayDate = calendar.getTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String startDate = formatter.format(yesterdayDate) + " 00:00:00";
			// 获取参数中的结束日期
			String endDate = formatter.format(yesterdayDate) + " 23:59:59";

			Map variables = new HashMap();
			//判断是cache 还是IRIR his数据库
			if (CdcConfigCache.me().getValueByKey("SDS", "his_dbtype", "").equals("IRIS")) {
				variables.put("his_driver", CdcConfigCache.me().getValueByKey("SDS","his_driver"));
				variables.put("his_url", CdcConfigCache.me().getValueByKey("SDS","his_url"));
			}
			// his服务器IP地址
			variables.put("his_ip", CdcConfigCache.me().getValueByKey("SDS", "his_ip"));
			// his数据库名称
			variables.put("his_ns", CdcConfigCache.me().getValueByKey("SDS", "his_ns"));
			// his数据库端口号
			variables.put("his_p", CdcConfigCache.me().getValueByKey("SDS", "his_p"));
			// his数据库用户名
			variables.put("his_u", CdcConfigCache.me().getValueByKey("SDS", "his_u"));
			// his数据库密码
			variables.put("his_pw", AesUtil.decode(CdcConfigCache.me().getValueByKey("SDS", "his_pw")));
			// sds服务器IP地址
			variables.put("sds_ip", CdcConfigCache.me().getValueByKey("SDS", "sds_ip"));
			// sds数据库端口号
			variables.put("sds_p", CdcConfigCache.me().getValueByKey("SDS", "sds_p"));
			// sds数据库名称
			variables.put("sds_s", CdcConfigCache.me().getValueByKey("SDS", "sds_s"));
			// sds数据库用户名
			variables.put("sds_u", CdcConfigCache.me().getValueByKey("SDS", "sds_u"));
			// sds数据库密码
			variables.put("sds_pw", AesUtil.decode(CdcConfigCache.me().getValueByKey("SDS", "sds_pw")));

			KettleUtil.runSDSJob("expense.kjb", variables, startDate,endDate, null, "1", false);
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		//先抽最新数据，再评估。
		String jobFile = "/Users/sunyingjie/Documents/工作文件/项目/单病种/imedical/medical-web/sys-home/kettle/paadmById/sds.kjb";
		//从his抽取病例个人数据
		try {
			KettleUtil.runPaAdmJob("78",jobFile,10);
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}
}
